﻿using PoemService.Shared;

namespace PoemGame.Domain.Services.Duishi
{
    public class DuishiCheckAnswerService : ICheckAnswerService
    {
        protected readonly IPoemService poemService;
        public DuishiCheckAnswerService(IPoemService _poemService)
        {
            poemService = _poemService;
        }
        public async Task<bool> CheckAnswer(CheckAnswerServiceInput game, string answer)
        {
            //首先获取上一玩家的句子
            var lastline = game.GameCondition;
            var records = game.ProperAnswers;
            if (records.Count > 0)
            {
                lastline = records[records.Count - 1];
            }
            //然后将该诗的句子找出
            List<PoemLine> lines;
            var poemline = await poemService.GetPoemLine(lastline);
            if (poemline != null)
            {
                lines = await poemService.GetPoemLineByPoemId(poemline.PoemId);
            }
            else return false;
            //找出下一句
            var line = lines.Find(o => o.Order > poemline.Order);
            if (line != null)
            {
                if (line.LineContent == answer)
                {
                    return true;
                }
                else return false;
            }
            else
            {
                //如果没有下一句，以输入的这一句开头
                var newpoemline = await poemService.GetPoemLine(answer);
                return newpoemline != null;
            }
        }

    }
}